package com.totcy.atom.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.totcy.atom.R;
import com.totcy.atom.bean.DailySentenceCiBa;
import com.totcy.atom.presenter.DailySentencePresenter;
import com.totcy.atom.presenter.contract.DailySentenceContract;
import com.totcy.atom.utils.UserUtil;
import com.totcy.atom.utils.glide.GlideUtil;
import com.totcy.baselibrary.utils.SPUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;


/**
 * description: 启动页
 *
 * @author: Totcy
 * date: 2017/10/31 18:55
 * update: 2017/10/31
 */

public class SplashActivity extends BaseFullActivity<DailySentenceContract.View, DailySentencePresenter> implements DailySentenceContract.View {

    @BindView(R.id.iv_splash)
    ImageView ivSplash;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //释放资源
        getWindow().setBackgroundDrawable(null);
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean isChildLayoutBelowTitle() {
        //全屏显示
        return false;
    }

    @Override
    public String getTitleName() {
        return "";
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        //隐藏标题栏
        getToolbarTitleLayout().setVisibility(View.GONE);
        getPresenter().getDailySentence();
    }

    /**
     * @param dailySentenceCiBa
     */
    @SuppressLint("CheckResult")
    @Override
    public void onDailySentenceCiBa(DailySentenceCiBa dailySentenceCiBa) {
        GlideUtil.loadImage(this, dailySentenceCiBa.getFenxiang_img(), ivSplash, true, R.drawable.splash_bg);
        if (getIntent().getBooleanExtra("justLook", false)) {
            return;
        }
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("");
            }
        })
                .delay(2500, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        String emailCount = UserUtil.getInstances().getUserName();
                        if (!TextUtils.isEmpty(emailCount)) {
                            startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        } else {
                            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        }
                        finish();
                    }
                });
    }

    /**
     * 这里获取数据失败
     *
     * @param code
     * @param message
     */
    @Override
    public void onDataFail(int code, String message) {
        if (getIntent().getBooleanExtra("justLook", false)) {
            return;
        }
        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        finish();
    }
}
